package com.wanghua.mall.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.user.entity.UserReceiveAddressEntity;
import com.wanghua.mall.user.service.UserReceiveAddressService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * 用户收货地址
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-24 18:51:22
 */
@RestController
@RequestMapping("/user/userreceiveaddress")
public class UserReceiveAddressController {

    @Resource
    private UserReceiveAddressService userReceiveAddressService;

    /**
     * 根据用户id获取他的收货地址
     *
     * @param userId
     * @return
     */
    @GetMapping("/getAddressByUserId/{userId}")
    public List<UserReceiveAddressEntity> getAddressByUserId(@PathVariable("userId") Long userId) {
        return userReceiveAddressService.getAddressByUserId(userId);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userReceiveAddressService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserReceiveAddressEntity userReceiveAddress = userReceiveAddressService.getById(id);
        return R.ok().put("userReceiveAddress", userReceiveAddress);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UserReceiveAddressEntity userReceiveAddress) {
        userReceiveAddressService.save(userReceiveAddress);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody UserReceiveAddressEntity userReceiveAddress) {
        userReceiveAddressService.updateById(userReceiveAddress);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userReceiveAddressService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
